"""
数据读取工具模块
提供各种格式测试数据的读取功能
"""

import csv
import os
from typing import List, Tuple, Any


def read_csv_data(csv_filename: str) -> List[Tuple[Any, ...]]:
    """
    从CSV文件读取测试数据
        csv_filename (str): CSV文件名（不包含路径）
        
    Returns:
        List[Tuple]: 测试数据列表，格式为[(username, password, expectedalert), ...]
    """
    test_data = []
    # 构建CSV文件的完整路径
    current_dir = os.path.dirname(os.path.abspath(__file__))
    project_root = os.path.dirname(current_dir)
    csv_file = os.path.join(project_root, 'test_data', csv_filename)
    
    try:
        with open(csv_file, 'r', encoding='utf-8') as file:
            reader = csv.DictReader(file)
            for row in reader:
                # 处理空值，将空字符串转换为None
                processed_row = []
                for value in row.values():
                    processed_value = None if value == '' else value
                    processed_row.append(processed_value)
                test_data.append(tuple(processed_row))
    except FileNotFoundError:
        print(f"CSV文件未找到: {csv_file}")
        return []
    except Exception as e:
        print(f"读取CSV文件时出错: {str(e)}")
        return []
    
    return test_data


def read_login_csv_data() -> List[Tuple[str, str, str]]:
    """
    专门读取登录测试数据的便捷方法
        List[Tuple]: 登录测试数据列表，格式为[(username, password, expectedalert), ...]
    """
    return read_csv_data('login_test_data.csv')


def read_device_model_csv_data() -> List[Tuple[str, str, str]]:
    """
    专门读取设备型号测试数据的便捷方法
        List[Tuple]: 设备型号测试数据列表，格式为[(device_type, model_name, model_desc), ...]
    """
    return read_csv_data('device_model_test_data.csv')


def read_service_rule_csv_data() -> List[Tuple[str, str, str, str, str, str, str]]:
    """
    专门读取业务规则测试数据的便捷方法
    处理后付费-上报业务量的复杂费率数据格式
        List[Tuple]: 业务规则测试数据列表，格式为[(rule_name, rule_type, min_cost, estimated_cost, rate_unit, rate_price, description), ...]
        对于后付费-上报业务量类型，rate_unit字段包含复杂的费率数据，格式为：业务码|单位|单价;业务码|单位|单价
    """
    return read_csv_data('service_rule_test_data.csv')